/**
 * This file is part of Wasagent.
 *
 * Wasagent is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Wasagent is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Wasagent. If not, see <http://www.gnu.org/licenses/>.
 *
 * WasModule.java : This class parses the passed parameters and builds
 *                  the neccessary objects to do the testing, and 
 *                  returns the test results. 
 *
 *
 */
package net.wait4it.wasagent;

import java.util.Map;

import javax.management.ObjectName;
import com.ibm.websphere.management.AdminClient;

import net.wait4it.wasagent.jmx.JmxService;
import net.wait4it.wasagent.tests.ServerNameTest;
import net.wait4it.wasagent.utils.Option;
import net.wait4it.wasagent.utils.Result;

/**
 * @author Yann Lambret
 *
 */
public class WasModule {

    private StringBuilder header = new StringBuilder();
    private StringBuilder message = new StringBuilder();
    private StringBuilder output = new StringBuilder();
    private ServerNameTest serverNameTest = new ServerNameTest();
    private String status = "OK";
    private int code = 0;
    private AdminClient client = null;
    private ObjectName perfMbean = null;
    private String wasModuleIdByHostnamePort;


    public boolean isThis(String wasModuleIdbyHostnamePort)
    {
        return this.wasModuleIdByHostnamePort.equals(wasModuleIdbyHostnamePort); 
    }

    public String run(Map<String, String> params) {
        header = new StringBuilder();
        message = new StringBuilder();
        output = new StringBuilder();
        status = "OK";
        code = 0;
        

        try {

            if(client == null)
            {
                this.wasModuleIdByHostnamePort = params.get("hostname") + ":" + params.get("port");
                client = JmxService.getClient(params);          // Create client and connect to server.
                perfMbean = JmxService.getPerfMbean(client);    // Get performancebean WebSphere:*,type=Perf (always the same)
                System.out.println("WasModule.java: WasModule created.");
            }
            else
                System.out.println("WasModule.java: WasModule reused.");

        } catch (Exception e) {
            return "3|" + e.getMessage()+";";
        }
        // Get the server name
        header.append(serverNameTest.run(client, perfMbean)).append(": "); 

        if(params.containsKey("help")) {
        // If help is requested, show the available tests and exit
            for (Option option : Option.values()) {
                output.insert(0,option.getName() + " - \n" );
            }
        }
        else {
        // Iterate through valid tests, check what tests are requested, and executes
        // them.
                  for (Option option : Option.values()) {
                     if (params.containsKey(option.getName())) {
                        checkResult(option.getTest().run(client, perfMbean, params.get(option.getName())));
                
                      }
               }

        if (status.equals("OK"))
            header.append("status ").append(status);
        else
            header.append("status ").append(status).append(" - ").append(message.toString());

        output.insert(0, header + "|");
        output.insert(0, code + "|");
        }
        return output.toString();
    }

    private void checkResult(Result result) {
        String out = "";
        String msg = "";

        switch (result.getStatus()) {
        case OK:
            out = result.getOutput();
            break;
        case WARNING:
            if (code < 1) { code = 1; status = "WARNING"; }
            msg = result.getMessage();
            out = result.getOutput();
            break;
        case CRITICAL:
            if (code < 2) { code = 2; status = "CRITICAL"; }
            msg = result.getMessage();
            out = result.getOutput();
            break;
        case UNKNOWN:
            code = 3;
            status = "UNKNOWN";
            msg = result.getMessage();
            break;
        }

        if (msg.length() > 0) {
            if (message.length() > 0)
                message.append(", ");
            message.append(msg);
        }

        if (out.length() > 0) {
            if (output.length() > 0)
                output.append(" ");
            output.append(out);
        }
    }

}
